package com.my.helper;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.my.bean.Note;
import com.my.constant.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther: 小鸥
 * @date:
 * @time: 16:11
 * @package: com.my.helper
 * @project_name: My Note
 */
//数据库的帮助类
public class NoteDbOpenHelper extends SQLiteOpenHelper {

    public NoteDbOpenHelper(Context context){
        super(context, Constant.DB_NAME,null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建表
        db.execSQL(Constant.CREATE_TABLE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    //添加记事
    public Long insertData(Note note){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("title",note.getTitle());
        values.put("content",note.getContent());
        values.put("create_time",note.getCreate());
        return db.insert(Constant.TABLE_NAME_NOTE,null,values);
    }

    //查找
    @SuppressLint("Range")
    public List<Note> queryAllFromDb(){

        SQLiteDatabase db = getReadableDatabase();
        List<Note> noteList = new ArrayList<>();
        Cursor cursor = db.query(Constant.TABLE_NAME_NOTE, null, null, null, null, null, null);
        if (cursor!=null){
            while (cursor.moveToNext()){
                String id = cursor.getString(cursor.getColumnIndex("id"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                String create_time = cursor.getString(cursor.getColumnIndex("create_time"));
                Note note = new Note();
                note.setId(id);
                note.setTitle(title);
                note.setContent(content);
                note.setCreate(create_time);

                noteList.add(note);
            }
            cursor.close();
        }
        return noteList;
    }

    public int updateData(Note note){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("title",note.getTitle());
        values.put("content",note.getContent());
        values.put("create_time",note.getCreate());
        return db.update(Constant.TABLE_NAME_NOTE,values,"id like ?",new String[]{note.getId()});
    }

    public int deleteFromDbById(String id){
        SQLiteDatabase db = getWritableDatabase();
        return db.delete(Constant.TABLE_NAME_NOTE,"id like ?",new String[]{id});
    }

    @SuppressLint("Range")
    public List<Note> queryFromDbByTitle(String title){
        if (TextUtils.isEmpty(title)){
            return queryAllFromDb();
        }
        SQLiteDatabase db = getReadableDatabase();
        List<Note> noteList = new ArrayList<>();
        Cursor cursor = db.query(Constant.TABLE_NAME_NOTE, null, "title like ?", new String[]{"%"+title+"%"}, null, null, null);
        if (cursor!=null){
            while (cursor.moveToNext()){
                String id = cursor.getString(cursor.getColumnIndex("id"));
                String title1 = cursor.getString(cursor.getColumnIndex("title"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                String createTime = cursor.getString(cursor.getColumnIndex("create_time"));

                Note note = new Note();
                note.setId(id);
                note.setTitle(title1);
                note.setContent(content);
                note.setCreate(createTime);
                noteList.add(note);
            }
            cursor.close();
        }
        return noteList;
    }
}
